package background

import (
	"individual/controllers"
	"individual/models"
	"strings"
	"time"
)

type UserController struct {
	controllers.BaseController
}

// InitUserinfo
// @Title InitUserinfo
// @Description 初始化用户信息
// @Param username formdata string true "username"
// @Param authusername formdata string true "authusername"
// @Param password formdata string true "password"
// @Param nickname formdata string true "nickname"
// @Success 201 初始化成功
// @Failure 403 初始化失败
// @router /initUserinfo/ [post]
func (c *UserController) InitUserinfo() {
	var user models.User
	user.Qq = ""
	user.Wechat = ""
	user.Github = ""
	user.Gitee = ""
	user.Weibo = ""
	user.Csdn = ""
	user.Email = ""
	user.Introduction = "这个人很懒,什么都没留下"
	user.Theme = "default"
	user.HeadPortrait = "https://img0.baidu.com/it/u=1356523179,1772235027&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=500"
	user.Nickname = c.GetString("nickname")
	user.Username = c.GetString("username")
	user.Password = c.GetString("password")
	authUsername := c.GetString("authusername")
	user.AuthUsername = authUsername
	user.Title = "美好的一天--从代码开始"
	user.CreatAt = time.Now()
	_, err := c.O.Insert(&user)
	if err != nil {
		c.Data["json"] = models.GetRevaErr(err, "初始化失败")
		c.ServeJSON()
		return
	}
	c.Data["json"] = models.GetRevaOk("初始化用户信息成功")
	c.ServeJSON()
}

// UpdateSocialLink
// @Title UpdateSocialLink
// @Description 更新社交链接
// @Param gitee formdata string true "gitee"
// @Param wechat formdata string true "wechat"
// @Param qq formdata string true "qq"
// @Param github formdata string true "github"
// @Param weibo formdata string true "weibo"
// @Param csdn formdata string true "csdn"
// @Param email formdata string true "email"
// @Success 201 更新社交链接成功
// @Failure 403 更新社交链接失败
// @router /updateSocialLink/ [put]
func (c *UserController) UpdateSocialLink() {
	csdn := c.GetString("csdn")
	gitee := c.GetString("gitee")
	wechat := c.GetString("wechat")
	qq := c.GetString("qq")
	email := c.GetString("email")
	github := c.GetString("github")
	weibo := c.GetString("weibo")
	var user = models.User{Id: 1, Email: email, Weibo: weibo, Qq: qq, Wechat: wechat, Github: github, Gitee: gitee, Csdn: csdn}
	_, err := c.O.Update(&user, "Email", "Csdn", "Weibo", "Qq", "Wechat", "Github", "Gitee")
	if err != nil {
		c.Data["json"] = models.GetRevaErr(err, "更改失败")
		c.ServeJSON()
		return
	}
	c.Data["json"] = models.GetRevaOk("更改成功")
	c.ServeJSON()
}

// UpdateTitle
// @Title UpdateTitle
// @Description 更新Title
// @Param title formdata string true "title"
// @Success 201 更新Title成功
// @Failure 403 更新Title失败
// @router /updateTitle/ [put]
func (c *UserController) UpdateTitle() {
	title := c.GetString("title")
	var user = models.User{Id: 1, Title: title}
	_, err := c.O.Update(&user, "Title")
	if err != nil {
		c.Data["json"] = models.GetRevaErr(err, "更新Title失败")
		c.ServeJSON()
		return
	}
	c.Data["json"] = models.GetRevaOk("更改title成功")
	c.ServeJSON()
}

// UpdateAnotherParma
// @Title UpdateAnotherParma
// @Description 更新介绍，主题，头像，昵称和个性签名
// @Param introduction formdata string false "介绍"
// @Param photo formdata string false "头像"
// @Param nickname formdata string true "昵称"
// @Param signature formdata string false "个性签名"
// @Success 201 更新介绍，主题，头像，昵称成功
// @Failure 403 更新介绍，主题，头像，昵称失败
// @router /updateAnotherParma/ [put]
func (c *UserController) UpdateAnotherParma() {
	introduction := c.GetString("introduction")
	photo := c.GetString("photo")
	if strings.Trim(photo, " ") == "" {
		photo = "https://img0.baidu.com/it/u=1356523179,1772235027&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=500"
	}
	nickname := c.GetString("nickname")
	signature := c.GetString("signature")
	var user = models.User{Id: 1, Introduction: introduction, HeadPortrait: photo, Nickname: nickname, Signature: signature}
	_, err := c.O.Update(&user, "Introduction", "HeadPortrait", "Nickname", "Signature")
	if err != nil {
		c.Data["json"] = models.GetRevaErr(err, "更新介绍，主题，头像，昵称失败")
		c.ServeJSON()
		return
	}
	c.Data["json"] = models.GetRevaOk("更改成功")
	c.ServeJSON()
}

// UpdateTheme
// @Title UpdateTheme
// @Description 更新主题
// @Param theme formdata string true "主题"
// @Success 201 更新主题成功
// @Failure 403 更新主题失败
// @router /updateTheme/ [put]
func (c *UserController) UpdateTheme() {
	theme := c.GetString("theme")
	var user = models.User{Id: 1, Theme: theme}
	_, err := c.O.Update(&user, "Theme")
	if err != nil {
		c.Data["json"] = models.GetRevaErr(err, "更改主题失败")
		c.ServeJSON()
		return
	}
	c.Data["json"] = models.GetRevaOk("更改主题成功")
	c.ServeJSON()
}

// AddTag
// @Title AddTag
// @Description 单独添加用户标签
// @Param tag formdata string true "用户标签"
// @Success 201 添加成功
// @Failure 403 添加失败
// @router /addTag/ [post]
func (c *UserController) AddTag() {
	var tag models.Tag
	tag.TagName = c.GetString("tag")
	id, err := c.O.Insert(&tag)
	if err != nil {
		c.Data["json"] = models.GetRevaErr(err, "添加用户标签失败")
		c.ServeJSON()
		return
	}
	tag.Id = id
	c.Data["json"] = models.GetRevaOkAndDes(tag, "添加用户标签成功")
	c.ServeJSON()
}

// DeleteTag
// @Title DeleteTag
// @Description 删除用户标签
// @Param id path int true "用户标签id"
// @Success 201 删除成功
// @Failure 403 删除失败
// @router /delTag/:id [delete]
func (c *UserController) DeleteTag() {
	id, err := c.GetInt64(":id")
	if err != nil {
		c.Data["json"] = models.GetRevaErr(err, "传入参数错误")
		c.ServeJSON()
		return
	}
	_, err = c.O.Delete(&models.Tag{Id: id})
	if err != nil {
		c.Data["json"] = models.GetRevaErr(err, "删除用户标签失败")
		c.ServeJSON()
		return
	}
	c.Data["json"] = models.GetRevaOk("删除用户标签成功")
	c.ServeJSON()
}

// GetTotalDataAWeek
// @Title GetTotalDataAWeek
// @Description 获取一周新增的留言数、阅读量
// @Success 201 获取一周新增的留言数、阅读量成功
// @Failure 403 获取一周新增的留言数、阅读量失败
// @router /getTotalDataAWeek/ [get]
func (c *UserController) GetTotalDataAWeek() {
	var b = models.Blog{}
	allViews, _ := b.GetAllViewsAWeek()
	week, _ := new(models.LeaveWord).GetAllNumsAWeek()
	c.Data["json"] = models.GetRevaOk(map[string]interface{}{
		"leaveWordNumAWeek": week,
		"AllViewsAWeek":     allViews,
	})
	c.ServeJSON()
}
